import { copy } from 'iclipboard'
import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import yd from './static/images/yd.png'
import load from './static/images/loading.gif';
import bg from './static/images/bg.png'
import wgjbtn from './static/images/wgjbtn.png'
import btn from './static/images/btn.png'
import logo from './static/images/logo.png'
import wgjbg from './static/images/wgjbg.jpg'
import wgjllog from './static/images/wgjllog.jpg'
const iswx = isWeiXin();
const isIos = navigator.userAgent.match(/(iPhone|iPod|iPad);?/i);
const isAndriod = navigator.userAgent.match(/android/i);
let grid = getParamValue('grid');
let apkUrl = getParamValue('apkUrl');
let appType = getParamValue('appType');
if (!appType || appType == undefined) {
    appType = getParamValue('apptype');
}
if (grid != '1' && appType && appType == 'GRID_HOME') {
    grid = 1;
}
let appStoreUrl = grid == '1' ? appConstants.gridIosAppStoreUrl : appConstants.gwIosAppStoreUrl;
let andriodSchema = grid == '1' ? appConstants.gridAndriodSchemaUrl : appConstants.gwAndriodSchemaUrl;
let iosSchema = grid == '1' ? appConstants.gridIosSchemaUrl : appConstants.gwIosProdSchemaUrl;
apkUrl = apkUrl == undefined || apkUrl === null || apkUrl.trim() === '' ? (grid == '1' ? appConstants.gridApkUrl : appConstants.apkUrl) : apkUrl;


function downloadApp() {
    //window.location.href='https://sj.qq.com/myapp/detail.htm?apkName=com.gong_wei';
    window.location.href = apkUrl;
}

class Comp extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false
        }
        this.accept = this.accept.bind(this);
    }

    accept () {
        copy(`inviteCode:${getParamValue('inviteCode')}`);
        this.setState({ loading: true })

        // let e = scheme.substring(0, scheme.indexOf("://"))
        // let n = scheme.replace(e, "intent") + "#Intent;scheme=" + e + ";end";

        window.launchApp(`?inviteCode=${getParamValue('inviteCode')}`,
            true,
            {
                PROTOCAL: {
                    ANDROID: andriodSchema,
					IOS: iosSchema
                },
                FAILBACK: {
                    ANDROID: apkUrl,
					IOS: appStoreUrl
                },
                LOAD_WAITING: 4000,
        }).then(() => {
            this.setState({ installd: true, loading: false });
        }).catch(() => {
            //downloadApp();
            this.setState({ loading: false })
        });
    }

    render() {
        const { installd, loading } = this.state;
        return (<>
            {iswx ? (
                    <div style={{
                        position: 'absolute',
                        zIndex: '50',
                        top: 0,
                        left: 0,
                        backgroundColor: 'rgba(0, 0, 0, 0.6)',
                        width: '100%',
                        height: '100%',
                        paddingTop: '20px',
                        boxSizing: 'border-box'
                        }}>
                        <div  style={{backgroundImage: `url(${yd})`,
                            margin: '0 auto',
                            width: '90%',
                            height: 'calc(100% - 20px)',
                            backgroundSize: '100% auto',
                            backgroundRepeat: 'no-repeat'}}></div>
                    </div>
                ) : null
            }
            <div style={{ display: loading ? 'block' : 'none', position: 'absolute', top: 0, left: 0,
                zIndex: 10, width: '100%', height: '100%', backgroundColor: 'rgba(0, 0, 0, 0.4)',
                backgroundImage: `url(${load})`, backgroundPosition: 'center', backgroundRepeat: 'no-repeat' }}></div>
            <div style={{ position: 'relative', height: '100%', backgroundImage: `url(${grid == '1' ? wgjbg : bg})`, backgroundPosition: grid == '1' ? 'top' : 'center',
                backgroundRepeat: 'no-repeat', backgroundSize: '100% auto' }}>
                <div style={{ position: 'absolute', top: '78%', left: 0, right: 0, width: '100%' }}>
                    <img src={grid == '1' ? wgjbtn : btn}
                         onClick={this.accept}
                         style={{ display: 'block', width: '60%', height: 'auto', margin: '0 auto 15px auto' }}/>
                    <img src={grid == '1' ? wgjllog : logo} style={{  display: 'block', width: grid == '1' ? '40%' : '15%', height: 'auto', margin: '0 auto' }}/>
                </div>
            </div>
        </>)
    }
}

ReactDOM.render(
    (<Comp/>),
    document.getElementById("app"));

function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    }
    else {
        return false;
    }
}
